﻿using System.Collections.Generic;
using System.Xml.Linq;
using Engine;
using Test1;

namespace Game
{
    public class MCCGPurchaseWidget : CanvasWidget
    {
        public ComponentMiner m_componentMiner;

        public ComponentTest1 m_componentTest1;

        public ListPanelWidget m_listPanelWidget;

        public GridPanelWidget m_inventoryGrid;

        public BevelledButtonWidget m_changeButton;

        public BevelledButtonWidget m_purchaseButton;

        public BevelledButtonWidget m_leftButton;

        public BevelledButtonWidget m_rightButton;

        public SliderWidget m_slider;

        public BlockIconWidget m_blockIcon;

        public LabelWidget m_selectedText;

        public LabelWidget m_pFlorin;

        public LabelWidget m_price;

        public LabelWidget m_categoryDisplay;

        public BevelledButtonWidget m_mpButton;

        public int SelectedValue;

        public int m_lastSelectedValue;

        public List<string> m_categories = new List<string>();

        public int CurrentCategoryIndex;

        public string CurrentCategory
        {
            get
            {
                if (CurrentCategoryIndex < 0 || CurrentCategoryIndex >= m_categories.Count)
                {
                    CurrentCategoryIndex = 0;
                }

                return m_categories[CurrentCategoryIndex];
            }
        }

        public MCCGPurchaseWidget(ComponentMiner componentMiner)
        {
            m_componentMiner = componentMiner;
            m_componentTest1 = m_componentMiner.Entity.FindComponent<ComponentTest1>();
            XElement node = ContentManager.Get<XElement>("Widgets/Systemshop/MCCGPurchaseWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_changeButton = Children.Find<BevelledButtonWidget>("ChangeButton");
            m_purchaseButton = Children.Find<BevelledButtonWidget>("PurchaseButton");
            m_leftButton = Children.Find<BevelledButtonWidget>("Left");
            m_mpButton = Children.Find<BevelledButtonWidget>("MPButton");
            m_rightButton = Children.Find<BevelledButtonWidget>("Right");
            m_slider = Children.Find<SliderWidget>("Slider");
            m_blockIcon = Children.Find<BlockIconWidget>("Icon");
            m_selectedText = Children.Find<LabelWidget>("SelectedText");
            m_pFlorin = Children.Find<LabelWidget>("PFlorin");
            m_price = Children.Find<LabelWidget>("Price");
            m_categoryDisplay = Children.Find<LabelWidget>("Category");
            int num = 10;
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++)
            {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++)
                {
                    InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(componentMiner.Inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }

            m_listPanelWidget = Children.Find<ListPanelWidget>("List");
            m_listPanelWidget.ItemWidgetFactory = delegate (object item)
            {
                int value = (int)item;
                int num2 = Terrain.ExtractContents(value);
                int num3 = Terrain.ExtractData(value);
                XElement node2 = ContentManager.Get<XElement>("Widgets/RecipaediaItem");
                ContainerWidget containerWidget = (ContainerWidget)Widget.LoadWidget(this, node2, null);
                BlockIconWidget blockIconWidget = containerWidget.Children.Find<BlockIconWidget>("RecipaediaItem.Icon");
                blockIconWidget.Value = value;
                blockIconWidget.Size = new Vector2(60f, 60f);
                LabelWidget labelWidget = containerWidget.Children.Find<LabelWidget>("RecipaediaItem.Text");
                LabelWidget labelWidget2 = containerWidget.Children.Find<LabelWidget>("RecipaediaItem.Details");
                labelWidget.Text = MoneyManager.GetBlockDisplayName(value);
                labelWidget.FontScale = 0.6f;
                labelWidget.DropShadow = true;
                labelWidget2.Text = string.Format("单价:{0} 出售:{1}" + MoneyManager.CoinChar, MoneyManager.GetItemMoney(value), MoneyManager.GetReturnMoney(value));
                labelWidget2.FontScale = 0.5f;
                labelWidget2.DropShadow = true;
                labelWidget2.Color = Color.Yellow;
                return containerWidget;
            };
            CurrentCategoryIndex = 0;
            m_categories.Add("全部");
            foreach (Money recipe in MoneyManager.m_recipes)
            {
                string category = BlocksManager.Blocks[Terrain.ExtractContents(recipe.ResultValue)].GetCategory(recipe.ResultValue);
                if (!m_categories.Contains(category))
                {
                    m_categories.Add(category);
                }
            }

            UpdateList();
        }

        public override void Update()
        {
            if (!m_componentMiner.IsAddedToProject || m_componentMiner.ComponentCreature.ComponentHealth.Health <= 0f)
            {
                base.ParentWidget.Children.Remove(this);
            }

            if (m_mpButton.IsClicked && m_componentTest1 != null)
            {
                float num = m_componentTest1.m_mp * m_componentTest1.m_Maxmp;
                if (m_componentTest1.m_subsystemGameInfo.WorldSettings.GameMode == GameMode.Creative)
                {
                    m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("提取灵能成功！", Color.White, blinking: true, playNotificationSound: false);
                    MoneyManager.AddItem(m_componentMiner, m_componentMiner.Project.FindSubsystem<SubsystemPickables>(), 987, 1000);
                }
                else if (num < 1000f)
                {
                    m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("提取失败，至少需要1000灵能才能提取！", Color.White, blinking: true, playNotificationSound: false);
                }
                else if (num >= 1000f)
                {
                    m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("提取灵能成功！", Color.White, blinking: true, playNotificationSound: false);
                    m_componentTest1.m_mp -= 1000f / m_componentTest1.m_Maxmp;
                    MoneyManager.AddItem(m_componentMiner, m_componentMiner.Project.FindSubsystem<SubsystemPickables>(), 987, 1000);
                }
            }

            if (m_listPanelWidget.SelectedItem != null)
            {
                SelectedValue = (int)m_listPanelWidget.SelectedItem;
            }
            else
            {
                SelectedValue = 0;
            }

            if (m_changeButton.IsClicked)
            {
                m_componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new MCCGSellWidget(m_componentMiner);
            }

            m_slider.MaxValue = ((SelectedValue <= 0) ? 10 : BlocksManager.Blocks[Terrain.ExtractContents(SelectedValue)].GetMaxStacking(SelectedValue));
            if (m_lastSelectedValue != SelectedValue)
            {
                m_slider.Value = 1f;
            }

            m_lastSelectedValue = SelectedValue;
            int itemCountInInventory = MoneyManager.GetItemCountInInventory(MoneyManager.CoinValue, m_componentMiner.Inventory);
            if (SelectedValue > 0)
            {
                int num2 = (int)m_slider.Value * MoneyManager.GetItemMoney(SelectedValue);
                m_slider.Text = m_slider.Value.ToString();
                m_blockIcon.Value = SelectedValue;
                m_selectedText.Text = $"{MoneyManager.GetBlockDisplayName(SelectedValue)}x{(int)m_slider.Value}";
                m_purchaseButton.Color = Color.White;
                m_price.Text = "-" + num2 + MoneyManager.CoinChar;
                if (m_purchaseButton.IsClicked)
                {
                    if (itemCountInInventory >= num2)
                    {
                        m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage($"已购买{m_slider.Value}个{MoneyManager.GetBlockDisplayName(SelectedValue)}，失去{num2}{MoneyManager.CoinChar}", Color.White, blinking: true, playNotificationSound: false);
                        MoneyManager.RemoveItemFromInventory(MoneyManager.CoinValue, num2, m_componentMiner.Inventory);
                        MoneyManager.AddItem(m_componentMiner, m_componentMiner.Project.FindSubsystem<SubsystemPickables>(), SelectedValue, (int)m_slider.Value);
                    }
                    else
                    {
                        m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("你的" + MoneyManager.CoinChar + "不够了", Color.White, blinking: true, playNotificationSound: false);
                    }
                }
            }
            else
            {
                m_slider.Text = "未选择";
                m_blockIcon.Value = 0;
                m_selectedText.Text = string.Empty;
                m_purchaseButton.Color = Color.Gray;
                m_price.Text = string.Empty;
            }

            m_pFlorin.Text = $"你的余额:{itemCountInInventory}{MoneyManager.CoinChar}";
            m_categoryDisplay.Text = LanguageControl.Get("BlocksManager", CurrentCategory);
            m_leftButton.IsEnabled = CurrentCategoryIndex > 0;
            m_rightButton.IsEnabled = CurrentCategoryIndex < m_categories.Count - 1;
            if (m_leftButton.IsEnabled && m_leftButton.IsClicked)
            {
                CurrentCategoryIndex = MathUtils.Max(0, CurrentCategoryIndex - 1);
                UpdateList();
            }

            if (m_rightButton.IsEnabled && m_rightButton.IsClicked)
            {
                CurrentCategoryIndex = MathUtils.Min(m_categories.Count - 1, CurrentCategoryIndex + 1);
                UpdateList();
            }
        }

        public void UpdateList()
        {
            m_slider.Value = 1f;
            m_listPanelWidget.ClearItems();
            m_listPanelWidget.ScrollPosition = 0f;
            foreach (Money recipe in MoneyManager.m_recipes)
            {
                string category = BlocksManager.Blocks[Terrain.ExtractContents(recipe.ResultValue)].GetCategory(recipe.ResultValue);
                if (CurrentCategory == "全部" || CurrentCategory == category)
                {
                    m_listPanelWidget.AddItem(recipe.ResultValue);
                }
            }
        }
    }
}